package com.matt.service.impl;

import com.alibaba.fastjson.JSON;
import com.dotflat.staffExtend.domain.StaffExtendCareer;
import com.dotflat.staffExtend.domain.StaffExtendInfo;
import com.dotflat.staffExtend.repository.StaffExtendCareerRepository;
import com.dotflat.staffExtend.repository.StaffExtendInfoRepository;
import com.matt.commons.dto.sso.AreaDTO;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.repository.StaffActionLogRepository;
import com.matt.service.StaffActionLogService;
import com.matt.service.remoteCall.DTO.RoleDTO;
import com.matt.service.remoteCall.SsoCallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/27
 * *******************************
 */
@Service
@Slf4j
public class StaffActionLogServiceImpl implements StaffActionLogService {


    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private  StaffActionLogRepository staffActionLogRepository;

    @Autowired
    private  StaffExtendCareerRepository staffExtendCareerRepository;

    @Autowired
    private  StaffExtendInfoRepository staffExtendInfoRepository;

    @Override
    public StaffExtendCareer save(StaffExtendCareer staffExtendCareer) {
        StaffExtendInfo staffExtendInfo = staffExtendInfo(staffExtendCareer.getAreaId(),staffExtendCareer.getId());
        staffExtendCareer.setExtendInfoId(staffExtendInfo.getId());
        return staffExtendCareerRepository.save(staffExtendCareer );
    }

    @Override
    public StaffExtendCareer  saveAreaRoles(String ticket, String staffId) {
        StaffDTO staffDTO = ssoCallService.getStaffDTO(ticket);
        staffExtendInfo(staffDTO.getAreaId(),staffDTO.getId());
        AreaDTO areaDTO = ssoCallService.getAreaDTO(ticket);
        StaffDTO staff = ssoCallService.getStaffDTO(null, staffId);
        List<RoleDTO>  list = ssoCallService.getStaffRoleList(ticket, staffId);
        List<String> roleNames = list.stream().map(k->k.getRoleName()).collect(Collectors.toList());
        String[] roleNames1 = roleNames.toArray(new String[roleNames.size()]);
        StaffExtendInfo staffExtendInfo = staffExtendInfo(staffDTO.getAreaId(),staffDTO.getId());
        return staffExtendCareerRepository.save(StaffExtendCareer.of(staffDTO.getAreaId(),staffDTO.getId(),staffExtendInfo.getId(),
                "("+staff.getRealName()+")管理，重置("+staff.getRealName()+")用户，("+areaDTO.getName()+")园区角色，重置后角色："+ JSON.toJSON(roleNames1)));

    }

    @Override
    public StaffExtendCareer save(String ticket, String description) {
        StaffDTO staffDTO = ssoCallService.getStaffDTO(ticket);
        StaffExtendInfo staffExtendInfo = staffExtendInfo(staffDTO.getAreaId(),staffDTO.getId());
        return staffExtendCareerRepository.save(StaffExtendCareer.of(staffDTO.getAreaId(),staffDTO.getId(),staffExtendInfo.getId(),description ));
    }


    @Override
    public StaffExtendCareer saveClassStaffs(String ticket, String description, String[] staffIds) {
        StaffDTO staffDTO = ssoCallService.getStaffDTO(ticket);
        List<StaffDTO> list = ssoCallService.getStaffDTOList(ticket,staffIds);
        List<String> names = list.stream().map(k->k.getRealName()).collect(Collectors.toList());
        String ss = String.join(",", names );
        StaffExtendInfo staffExtendInfo = staffExtendInfo(staffDTO.getAreaId(),staffDTO.getId());
        return staffExtendCareerRepository.save(StaffExtendCareer.of(staffDTO.getAreaId(),staffDTO.getId(),staffExtendInfo.getId(),description+":("+ss +")") );
    }

    @Override
    public List<StaffExtendCareer> saveList(List<StaffExtendCareer> logList) {
        for (int i = 0; i < logList.size(); i++) {
            StaffExtendInfo staffExtendInfo = staffExtendInfo(logList.get(i).getAreaId(),logList.get(i).getExtendInfoId());
            logList.get(i).setExtendInfoId(staffExtendInfo.getId());
        }
        return staffExtendCareerRepository.saveAll(logList);
    }

    @Override
    public StaffExtendCareer findByAreaIdAndReserved(String areaId, String reserved) {
        return staffExtendCareerRepository.findByAreaIdAndReserved(areaId, reserved);
    }


    public  StaffExtendInfo staffExtendInfo(String areaId,String staffId){
        StaffExtendInfo staffExtendInfo = staffExtendInfoRepository.findFirstByAreaIdAndStaffId(areaId,staffId);
        if (Objects.isNull(staffExtendInfo)){
            staffExtendInfo= new StaffExtendInfo();
            staffExtendInfo.setStaffId(staffId);
            staffExtendInfo.setAreaId(areaId);
            staffExtendInfo.setCreatedBy(staffId);
            staffExtendInfo = staffExtendInfoRepository.save(staffExtendInfo);
        }
        return staffExtendInfo;
    }

//    @Override
//    public StaffExtendCareer findByAreaIdAndReserved(String areaId, String reserved) {
//        return staffExtendCareerRepository.findByAreaIdAndReserved(areaId,reserved);
//    }
}
